package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostTypeDao;
import ysq.xyg.host.model.po.HostType;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostTypeVo;
import ysq.xyg.host.service.HostTypeService;
import ysq.xyg.web.model.po.WebColumnType;
import ysq.xyg.web.model.vo.WebColumnTypeVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房产模块-房屋分类service实现
 *
 * @author shengQiang yu
 * @date 2019-7-1 15:24:55
 */
@Service("hostTypeService")
public class HostTypeServiceImpl extends BaseServiceImpl<HostType> implements HostTypeService {

    @Resource
    private HostTypeDao hostTypeDao;

    private HostType hostType;

    @Autowired
    public void setBaseDao(HostTypeDao hostTypeDao) {
        this.baseDao = hostTypeDao;
    }


    /**
     * 分页查询
     *
     * @param hostTypeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:24:55
     */
    public ReturnEntity page(HostTypeVo hostTypeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc, update_time desc");
        List<HostType> list = acceptList(hostTypeVo);
        PageInfo<HostType> page=new PageInfo<HostType>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostTypeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:24:55
     */
    public ReturnEntity addOrUpdate(HostTypeVo hostTypeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostTypeVo.getId())){
            hostType=new HostType(currentUser);
            isUpate=false;
        }else{
            hostType=selectByPrimaryKey(hostTypeVo.getId());
            if(ObjectUtil.isNull(hostType)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostType.setUpdateTime(new Date());
            hostType.setUpdateUser(currentUser.getId());
        }
        hostType.setName(hostTypeVo.getName());
        hostType.setDescription(hostTypeVo.getDescription());
        hostType.setSort(hostTypeVo.getSort());
        hostType.setShowType(hostTypeVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostType);
        }else{
            num=insert(hostType);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:24:55
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostType hostType=selectByPrimaryKey(id);
        if(hostType!=null){
            hostType.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostType.setUpdateTime(new Date());
            hostType.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostType);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostTypeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 15:24:55
     */
    public List<HostType> acceptList(HostTypeVo hostTypeVo){
        Example example = new Example(HostType.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostTypeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostTypeVo.getRemoveTag());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return hostTypeDao.selectByExample(example);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity acceptSelect(String code) {
        List<HostType> list = acceptList(new HostTypeVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(HostType hostType:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(hostType.getId());
            sysDictContent.setName(hostType.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(hostType.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
